import * as glob from "glob"
import { desktopPath } from "@/utils"
import { useKoa, useNode } from "@/hooks"

const { createRouter } = useKoa()
const { path } = useNode()

const router = createRouter()

const globOptions = { windowsPathsNoEscape: true }

router.all("/status", (ctx, next) => {
  const foldersPattern = path.join(desktopPath, "t_*/")

  const folders = glob.sync(foldersPattern, globOptions)

  const list = folders.map(folder => {
    const subFolders = glob.sync(path.join(folder, "*/"), globOptions)

    return {
      [path.basename(folder)]: subFolders,
    }
  })

  ctx.body = {
    status: 200,
    data: { list },
  }

  next()
})

export default router.routes()
